package com.situ.student.dao.impl;

import com.situ.student.common.Global;
import com.situ.student.dao.UserDao;
import com.situ.student.model.User;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class UserDaoImpl implements UserDao {
    private final RowMapper<User> mapper = new BeanPropertyRowMapper<>(User.class);

    @Override
    public User findByUsername(String username) {
        JdbcTemplate jt = Global.getJdbcTemplate();
        String sql = "select id,username,password from t_security_user where username=?";

        /*User user = null;
        try {
            user = jt.queryForObject(sql, mapper, username);
            return user;
        } catch (DataAccessException e) {
            return null;
        }*/

        List<User> users = jt.query(sql, mapper, username);
        return users.isEmpty() ? null : users.get(0);
    }
}
